/* Context switch
 *
 *   void swtch(struct context *old, struct context *new);
 *
 * Save current register context on the stack,
 * creating a struct context, and save its address in *old.
 * Switch stacks to new and pop previously-saved registers.
 */
.global swtch

swtch:
    stp x30, x29, [x0], #16
    stp x28, x27, [x0], #16
    stp x26, x25, [x0], #16
    stp x24, x23, [x0], #16
    stp x22, x21, [x0], #16
    stp x20, x19, [x0], #16
    stp x18, x17, [x0], #16
    mov x2, sp
    stp x16, x2, [x0]

    ldp x30, x29, [x1], #16
    ldp x28, x27, [x1], #16
    ldp x26, x25, [x1], #16
    ldp x24, x23, [x1], #16
    ldp x22, x21, [x1], #16
    ldp x20, x19, [x1], #16
    ldp x18, x17, [x1], #16
    ldp x16, x2, [x1]
    mov sp, x2

    ret